package com.cjd.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/10/4
 * description 防伪水印 文字
 */
public class WaterMarkDrawable extends Drawable {

    private Paint paint;
    private List<String> labels;
    private int degrees = -30;
    private int fontColor = Color.WHITE;
    private int colorBg = Color.TRANSPARENT;
    private int fontSize = 12;


    /**
     * @param context 上下文
     * @param labels  文件
     */
    public WaterMarkDrawable(Context context, List<String> labels) {
        this.labels = labels;
        init(context);
    }

    public WaterMarkDrawable(Context context, List<String> labels, int fontColor) {
        this.labels = labels;
        this.fontColor = fontColor;
        init(context);
    }

    public WaterMarkDrawable(Context context, List<String> labels, int fontColor,int fontSize) {
        this.labels = labels;
        this.fontColor = fontColor;
        this.fontSize = fontSize;
        init(context);
    }


    /**
     * @param context   上下文
     * @param labels    文字
     * @param degrees   角度
     * @param fontColor 字体颜色
     * @param colorBg   背景颜色
     * @param fontSize  字体大小
     */
    public WaterMarkDrawable(Context context, List<String> labels, int degrees, int fontColor, int colorBg, int fontSize) {
        this.labels = labels;
        this.degrees = degrees;
        this.fontColor = fontColor;
        this.colorBg = colorBg;
        this.fontSize = fontSize;
        init(context);
    }

    private void init(Context context) {
        paint = new Paint();
        paint.setColor(fontColor);
        paint.setAntiAlias(true);
        paint.setTextSize(sp2px(context, fontSize));
    }


    @Override
    public void draw(@NonNull Canvas canvas) {
        int width = getBounds().right;
        int height = getBounds().bottom;
        canvas.drawColor(colorBg);

        canvas.save();
        canvas.rotate(degrees);
        float textWidth = paint.measureText(getMaxLengthText());
        int textHeight = (int) getFontSizeHeight(paint);
        int index = 0;
        int segmentHeight = (textHeight * labels.size()) * 4;//间隔

        for (int positionY = segmentHeight; positionY <= height; positionY += segmentHeight) {
            float fromX = -width + (index++ % 2) * textWidth;
            for (float positionX = fromX; positionX < width; positionX += textWidth * 2) {
                int spacing = 0;//文字间距
                for (String label : labels) {
                    canvas.drawText(label, positionX, positionY + spacing, paint);
                    spacing = spacing + textHeight;
                }

            }
        }
        canvas.restore();
    }

    /**
     * 文字基准线的下部距离-文字基准线的上部距离 = 文字高度
     */
    private float getFontSizeHeight(Paint paint) {
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        return fontMetrics.descent - fontMetrics.ascent;
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int i) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }

    private String getMaxLengthText() {
        String text = labels.get(0);
        for (String str : labels) {
            if (text.length() > str.length()) {
                text = str;
            }
        }
        return text;
    }

    private int sp2px(Context context, int spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
}
